"""
见文件夹附录_git
"""
print("hello Git world!")
print("Hello everyone.")

# 配置 git
"""
git config --list 
git config --global user.name "sandman"
git config --global user.mail "1162218578@qq.com"
git config --global init.defaultBranch main
"""

# 创建一个项目 如本项目 hello_git.py

# .gitignore 忽略文件
"""用来配置不需要 git 追踪的文件
包含：
__pycache__/
.DS_Store   ##macos 需要添加的
"""

# 初始化仓库
# git init
"""
选定目录后输入 git init 生成.git文件用来存储 git 管理仓库的文件
"""

# 检查状态
#git status
"""git status

liuwei@liuweideMacBook-Pro 附录_git % git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.gitignore
	hello_git.py

nothing added to commit but untracked files present (use "git add" to track)
"""

# 将文件加入仓库
# git add .
"""
liuwei@liuweideMacBook-Pro 附录_git % git add .
liuwei@liuweideMacBook-Pro 附录_git % git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   .gitignore
	new file:   hello_git.py

"""

# 执行提交
# git commit -m/-am
"""
liuwei@liuweideMacBook-Pro 附录_git % git commit -m "Started project."
[main (root-commit) aa43b5d] Started project.
 2 files changed, 3 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 hello_git.py
liuwei@liuweideMacBook-Pro 附录_git % git status
On branch main
nothing to commit, working tree clean

"""

# 查看提交历史
# git log
## git log -p [最近 x 个comment次数]查看详细修改
## git log --stat  查看修改概要
## git show 同 git log -p
"""
liuwei@liuweideMacBook-Pro 附录_git % git log
commit aa43b5dafd7edcfddaa541eb82658258879573c0 (HEAD -> main)
Author: sandman <1162218578@qq.com>
Date:   Wed Jul 3 21:09:22 2024 +0800

    Started project.
liuwei@liuweideMacBook-Pro 附录_git % 

# --pretty=oneline只显示提交引用 ID 以及提交记录的信息



liuwei@liuweideMacBook-Pro 附录_git % git log --pretty=oneline
aa43b5dafd7edcfddaa541eb82658258879573c0 (HEAD -> main) Started project.
liuwei@liuweideMacBook-Pro 附录_git % 
"""

# 第二次提交
# git status
# git -am
# -am 表示git add + git commit，如果有新文件则需要手动 git add 加入到暂存区
"""
liuwei@liuweideMacBook-Pro 附录_git % git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   hello_git.py

no changes added to commit (use "git add" and/or "git commit -a")
liuwei@liuweideMacBook-Pro 附录_git % 


# -am 表示git add + git commit，如果有新文件则需要手动 git add 加入到暂存区
liuwei@liuweideMacBook-Pro 附录_git % git commit -am "Extended greeting."
[main f522d56] Extended greeting.
 1 file changed, 89 insertions(+), 1 deletion(-)
liuwei@liuweideMacBook-Pro 附录_git % 


liuwei@liuweideMacBook-Pro 附录_git % git status
On branch main
nothing to commit, working tree clean


liuwei@liuweideMacBook-Pro 附录_git % git log 
commit f522d56419c1ae0c82cfe5b2621a0e27dda9e8dc (HEAD -> main)
Author: sandman <1162218578@qq.com>
Date:   Wed Jul 3 21:19:30 2024 +0800

    Extended greeting.

commit aa43b5dafd7edcfddaa541eb82658258879573c0
Author: sandman <1162218578@qq.com>
Date:   Wed Jul 3 21:09:22 2024 +0800

    Started project.
liuwei@liuweideMacBook-Pro 附录_git % 
"""


# 放弃修改 恢复到上一个提交状态
# git status
# git restore .     就是放弃当前的修改，就是撤销任何未放到暂存区的修改
# git status
# git restore --staged .  就是撤销暂存，即撤销 commit 操作
"""
liuwei@liuweideMacBook-Pro 附录_git % git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   hello_git.py

no changes added to commit (use "git add" and/or "git commit -a")


liuwei@liuweideMacBook-Pro 附录_git % git restore .
liuwei@liuweideMacBook-Pro 附录_git % git status
On branch main
nothing to commit, working tree clean
liuwei@liuweideMacBook-Pro 附录_git % 

"""

# 检出以前的提交
# git log
# git checkout   回到指定版本，查看该版本内容，可以通过 git switch -回到上一步，或者 git checkout/switch main
    # 会丢弃你checkout 版本中任何未提交的修改
# git switch - 回到上一步
# git checkout -b 和 git switch -c 可以创建新分支并切换过去
"""
liuwei@liuweideMacBook-Pro 附录_git % git log

commit 624be2fd15cc98d22aca978b23f3032d4bed010d (HEAD -> main)
Author: sandman <1162218578@qq.com>
Date:   Wed Jul 3 21:30:06 2024 +0800

    将笔记单独记录

commit 5e89de08a69875187da045392e6719f2acf18644
Author: sandman <1162218578@qq.com>
Date:   Wed Jul 3 21:26:14 2024 +0800

    after learn git restore

commit f522d56419c1ae0c82cfe5b2621a0e27dda9e8dc
Author: sandman <1162218578@qq.com>
Date:   Wed Jul 3 21:19:30 2024 +0800

    Extended greeting.

commit aa43b5dafd7edcfddaa541eb82658258879573c0
Author: sandman <1162218578@qq.com>
Date:   Wed Jul 3 21:09:22 2024 +0800

    Started project.
    
    

liuwei@liuweideMacBook-Pro 附录_git % git status
On branch main
nothing to commit, working tree clean


liuwei@liuweideMacBook-Pro 附录_git % git checkout 5e89
Note: switching to '5e89'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 5e89de0 after learn git restore
liuwei@liuweideMacBook-Pro 附录_git % 

"""

## 撤销上一个操作
"""
liuwei@liuweideMacBook-Pro 附录_git % git switch -
Previous HEAD position was 5e89de0 after learn git restore
Switched to branch 'main'

"""

# 将项目恢复到指定提交版本
## git reset
# --hard完全回退（提交记录以及文件内容）   撤销暂存区、工作去、index
# --mixed（默认）回退提交不回退文件内容  如果再次add + commit 就变回撤销之前了    撤销暂存区和 index
# --soft 只回退HEAD 指针，如果再次 commit 就变回撤销之前了                撤销index
# git revert撤销指定分支的修改了的内容，后续的提交保留
"""
liuwei@liuweideMacBook-Pro 附录_git % git status
On branch main
nothing to commit, working tree clean
liuwei@liuweideMacBook-Pro 附录_git % 


liuwei@liuweideMacBook-Pro 附录_git % git log
commit 624be2fd15cc98d22aca978b23f3032d4bed010d (HEAD -> main)
Author: sandman <1162218578@qq.com>
Date:   Wed Jul 3 21:30:06 2024 +0800

    将笔记单独记录

commit 5e89de08a69875187da045392e6719f2acf18644
Author: sandman <1162218578@qq.com>
Date:   Wed Jul 3 21:26:14 2024 +0800

    after learn git restore

commit f522d56419c1ae0c82cfe5b2621a0e27dda9e8dc
Author: sandman <1162218578@qq.com>
Date:   Wed Jul 3 21:19:30 2024 +0800

    Extended greeting.

commit aa43b5dafd7edcfddaa541eb82658258879573c0
Author: sandman <1162218578@qq.com>
Date:   Wed Jul 3 21:09:22 2024 +0800

    Started project.
    
    
    
liuwei@liuweideMacBook-Pro 附录_git % git reset --hard 5e89
HEAD is now at 5e89de0 after learn git restore
liuwei@liuweideMacBook-Pro 附录_git % 


liuwei@liuweideMacBook-Pro 附录_git % git status
On branch main
nothing to commit, working tree clean


liuwei@liuweideMacBook-Pro 附录_git % git log
commit 5e89de08a69875187da045392e6719f2acf18644 (HEAD -> main)
Author: sandman <1162218578@qq.com>
Date:   Wed Jul 3 21:26:14 2024 +0800

    after learn git restore

commit f522d56419c1ae0c82cfe5b2621a0e27dda9e8dc
Author: sandman <1162218578@qq.com>
Date:   Wed Jul 3 21:19:30 2024 +0800

    Extended greeting.

commit aa43b5dafd7edcfddaa541eb82658258879573c0
Author: sandman <1162218578@qq.com>
Date:   Wed Jul 3 21:09:22 2024 +0800

    Started project.
liuwei@liuweideMacBook-Pro 附录_git % 

"""

# 删除仓库 如果提交记录混乱重新进行记录
# rm -rf .git/
"""
git status
rm -rf .git/
git status
git init
git status
git add
git commit -m "xxx"
git status
"""

"""
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
"""

# 从远程仓库拉取到本地
# git clone https://gitee.com/lw0110/learn_git.git


# 使用 gitee 做示例
"""
1、导入 ssh 公钥
本地ssh-keygen -t rsa生成 ssh 密钥对
然后查看 ~/.ssh下的 xxx.rsa.pub文件把内容复制到 gitee 中的 ssh 公钥中

2、gitee 创建仓库
3、git clone 克隆到本地
"""

# 提交
# git push
# git push -u 指定 git push 默认仓库
"""
liuwei@liuweideMacBook-Pro 附录_git % git push -u https://gitee.com/lw0110/learn_git.git
branch 'main' set up to track 'https://gitee.com/lw0110/learn_git.git/main'.
Everything up-to-date
liuwei@liuweideMacBook-Pro 附录_git % 
liuwei@liuweideMacBook-Pro 附录_git % git config -l
credential.helper=osxkeychain
init.defaultbranch=main
safe.directory=/opt/homebrew/Library/Taps/homebrew/homebrew-core
safe.directory=/opt/homebrew/Library/Taps/homebrew/homebrew-cask
user.name=sandman
user.email=1162218578@qq.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
branch.main.remote=https://gitee.com/lw0110/learn_git.git
branch.main.merge=refs/heads/main
liuwei@liuweideMacBook-Pro 附录_git % 
"""

# 分支
## git branch 列出所有本地分支
## git branch -r列出所有远程分支
## git branch [branch-name] 新建分支但留在当前分支
## git checkout -b branch 新建一个分支并且换到改分支
## git merge [branch] 合并指定分支到当前分支
## git branch -d [branch-name] 删除分支
## git push origin --delete [branch-name]
## git branch -dr [remote/branch]

"""
liuwei@liuweideMacBook-Pro 附录_git % git branch dev
liuwei@liuweideMacBook-Pro 附录_git % git branch
  dev
* main
liuwei@liuweideMacBook-Pro 附录_git % git branch -d dev
Deleted branch dev (was b34010d).
liuwei@liuweideMacBook-Pro 附录_git % git branch
* main
liuwei@liuweideMacBook-Pro 附录_git % 

"""
### git rm等价于 rm + git add

# git merge 分支合并
## 修改冲突地方 commit
## git log --graph --pretty=oneline --abbrev-commit
"""
添加删除文件都会合并到主线，git log 也会合并到主线
“Fast-forward”模式就是把指针直接挪到了 dev 分支上
"""
##合并冲突
"""
liuwei@liuweideMacBook-Pro appendix_git % git merge dev_conflict_samemodify
Auto-merging hello_git.py
CONFLICT (content): Merge conflict in hello_git.py
Automatic merge failed; fix conflicts and then commit the result.
liuwei@liuweideMacBook-Pro appendix_git % 
liuwei@liuweideMacBook-Pro appendix_git % git status
On branch main
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   hello_git.py

no changes added to commit (use "git add" and/or "git commit -a")
liuwei@liuweideMacBook-Pro appendix_git % ls
hello_git.py
liuwei@liuweideMacBook-Pro appendix_git % cat hello_git.py 
print("hello Git world!")
<<<<<<< HEAD
print("Hello EVERYONE.")
=======
print("Hello EVEryone.")
>>>>>>> dev_conflict_samemodify
liuwei@liuweideMacBook-Pro appendix_git % 

liuwei@liuweideMacBook-Pro appendix_git % vi hello_git.py 
liuwei@liuweideMacBook-Pro appendix_git % 
liuwei@liuweideMacBook-Pro appendix_git % cat hello_git.py         
print("hello Git world!")
print("Hello EVERyONE.")
liuwei@liuweideMacBook-Pro appendix_git % git commit -am "EVERyONE"
[main 0a7e348] EVERyONE
liuwei@liuweideMacBook-Pro appendix_git % git log
commit 0a7e348fa678dfbc8a17d7c075fbdefddc1201e1 (HEAD -> main)
Merge: 4a74d7c f818c65
Author: sandman <1162218578@qq.com>
Date:   Tue Jul 9 12:02:55 2024 +0800

    EVERyONE

commit 4a74d7c0c09b88e5d3dba7e6d7a43960675ae2df
Author: sandman <1162218578@qq.com>
Date:   Tue Jul 9 12:00:11 2024 +0800

    修改了 EVERYONE

commit f818c653dc8eb05d375a16907210dad199e21ae7 (dev_conflict_samemodify)
Author: sandman <1162218578@qq.com>
Date:   Tue Jul 9 11:59:34 2024 +0800

    大写了 EVE

commit b34010d4fdbe689c2c2bb2ce8d8dd497e62ab7a2
Author: sandman <1162218578@qq.com>
Date:   Sun Jul 7 23:37:14 2024 +0800

    将笔记单独提取记录

commit 5e89de08a69875187da045392e6719f2acf18644
Author: sandman <1162218578@qq.com>
Date:   Wed Jul 3 21:26:14 2024 +0800

    after learn git restore

commit f522d56419c1ae0c82cfe5b2621a0e27dda9e8dc
Author: sandman <1162218578@qq.com>
Date:   Wed Jul 3 21:19:30 2024 +0800

    Extended greeting.

commit aa43b5dafd7edcfddaa541eb82658258879573c0
Author: sandman <1162218578@qq.com>
Date:   Wed Jul 3 21:09:22 2024 +0800

    Started project.
liuwei@liuweideMacBook-Pro appendix_git % git sattus
git: 'sattus' is not a git command. See 'git --help'.

The most similar command is
	status
liuwei@liuweideMacBook-Pro appendix_git % git status
On branch main
nothing to commit, working tree clean
liuwei@liuweideMacBook-Pro appendix_git % 


"""


## 禁用 Fast forward模式，防止删掉分之后丢失分支信息
## git merge --no-ff -m "merge dev_delete no-ff" dev_delete

"""
liuwei@liuweideMacBook-Pro appendix_git % git merge --no-ff -m "merge dev_delete no-ff" dev_delete
Merge made by the 'ort' strategy.
 hello_git.py | 2 --
 1 file changed, 2 deletions(-)
 delete mode 100644 hello_git.py
liuwei@liuweideMacBook-Pro appendix_git % 

liuwei@liuweideMacBook-Pro appendix_git % git log --graph --pretty=oneline --abbrev-commit
*   50c884a (HEAD -> main) merge dev_delete no-ff
|\  
| * a68f590 (dev_delete) 在 dev_delete分支删除了 hello.py
|/  
* b34010d 将笔记单独提取记录
* 5e89de0 after learn git restore
* f522d56 Extended greeting.
* aa43b5d Started project.
liuwei@liuweideMacBook-Pro appendix_git % 

"""

## git stash 暂存dev未完成的任务,切换到 master 拉分支修 bug
## git stash list 查看暂存列表
##  git stash pop 恢复暂存内容
### 等价于 git stash apply + git stash drop  apply 后不删除 stash 内容
### 可以指定list中的记录恢复到指定 stash
"""
liuwei@liuweideMacBook-Pro appendix_git % git switch -c dev_stash
Switched to a new branch 'dev_stash'
liuwei@liuweideMacBook-Pro appendix_git % vi hello_git.py 
liuwei@liuweideMacBook-Pro appendix_git % git status
On branch dev_stash
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   .hello_git.py.swp

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	deleted:    .hello_git.py.swp
	modified:   hello_git.py

liuwei@liuweideMacBook-Pro appendix_git % git stash
Saved working directory and index state WIP on dev_stash: b34010d 将笔记单独提取记录
liuwei@liuweideMacBook-Pro appendix_git % git status
On branch dev_stash
nothing to commit, working tree clean
liuwei@liuweideMacBook-Pro appendix_git % 


==========修改 main 分支的 bug 并将修复分支合并到 main
========== 切换回 dev 分支
liuwei@liuweideMacBook-Pro appendix_git % git stash list
stash@{0}: WIP on dev_stash: b34010d 将笔记单独提取记录
liuwei@liuweideMacBook-Pro appendix_git % 
liuwei@liuweideMacBook-Pro appendix_git % git stash pop  
On branch dev_stash
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   .hello_git.py.swp

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   hello_git.py

Dropped refs/stash@{0} (d3197807a0b77bcc6ccbdca0996785fcfbe3870f)
liuwei@liuweideMacBook-Pro appendix_git % git stash list
liuwei@liuweideMacBook-Pro appendix_git % 


liuwei@liuweideMacBook-Pro appendix_git % git stash pop stash@{3}
On branch dev_stash
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   .hello_git.py.swp

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   hello_git.py

Dropped stash@{3} (f00cfec25e4784496d18d95a6a9335f507580ed8)
liuwei@liuweideMacBook-Pro appendix_git % cat hello_git.py 
print("hello Git world!")
print("Hello everyone.")
print("test stash")
liuwei@liuweideMacBook-Pro appendix_git % git stash list
stash@{0}: WIP on dev_stash: b34010d 将笔记单独提取记录
stash@{1}: WIP on dev_stash: b34010d 将笔记单独提取记录
stash@{2}: WIP on dev_stash: b34010d 将笔记单独提取记录
liuwei@liuweideMacBook-Pro appendix_git % 

"""



## bug修复片段复制到 dev 分支中
## git cherry-pick "comment_id" comment id 可以通过 git log 查询将 bugfix 分支的 commit 复制到本分支
"""
liuwei@liuweideMacBook-Pro appendix_git % cat hello_git.py 
print("hello Git world!")
print("Hello everyone.")
liuwei@liuweideMacBook-Pro appendix_git % git cherry-pick 98ff8b
[dev_stash 70291be] fixbug 01 Everyone
 Date: Tue Jul 9 12:14:46 2024 +0800
 1 file changed, 1 insertion(+), 1 deletion(-)
liuwei@liuweideMacBook-Pro appendix_git % git log
commit 70291be2e667896d32cbaea574b1784b17ffeb4a (HEAD -> dev_stash)
Author: sandman <1162218578@qq.com>
Date:   Tue Jul 9 12:14:46 2024 +0800

    fixbug 01 Everyone


"""



# 关于本地与远程仓库链接说明
## git remote -v 查看远程仓库配置
## git push -u 为了 push 和 pull 自动选取远程仓库相同名称的分支

"""
##
git clone直接拉取远程仓库自动建立联系,git remote -v

##
如果本地先有内容，git branch -r -a 都看不到远程仓库
需要先 git remote add origin [仓库地址]，git remote -v 可以看到配置了
然后 git fetch origin 命令从远程仓库拉取最新的分支信息。这个命令不会改变你当前的工作目录或你的任何本地分支，
    它只是从远程仓库下载最新的元数据和对象（包括分支信息）
再git branch -r就可以看到远程仓库了
"""

# git diff 显示未 git add 的
## git diff --cached 显示已经 git add 未 commit 的修改
### 在 diff 中如果只删除了这行的内容则会显示 - xxxx ，如果这一行（换行符也删除了），则会显示上一样删除然后又添加
    ### 类似下边的例子（+ -print("test_checkout")）
"""
liuwei@liuweideMacBook-Pro 附录_git % git diff
diff --git a/hello_git.py b/hello_git.py
index f5cf701..398a16f 100644
--- a/hello_git.py
+++ b/hello_git.py
@@ -1,3 +1,6 @@
 print("hello Git world!")
+print("test_diff3")
 print("Hello everyone.")
-print("test_checkout")
\ No newline at end of file
+print("test_checkout")
+print("test_diff")
+print("test_diff2")
\ No newline at end of file
liuwei@liuweideMacBook-Pro 附录_git % git diff --cached
liuwei@liuweideMacBook-Pro 附录_git % 
liuwei@liuweideMacBook-Pro 附录_git % 
"""

# git rm 从暂存区和工作区中删除
## git rm --cached 只从暂存区中删除，保留工作区

# git mv  重命名文件名或目录名，使用这个命令让 git 可以追踪。
    ### 做的工作就是 git rm --cached 然后再 git add 将新名字添加到暂存区




# 子模块
##添加子模块
"""
git submodule add https://地址  <本地子仓库路径>
子仓库路径需要为空让 git 自己创建
会多一个 .gitmodules的文件
"""

## git clone --recursive https://xxxxx  会同时执行子模块的更新
## 上行等价于
"""
git clone https://主仓库
此时子模块为空
git submodule init
git submodule update
此时子模块更新
"""

## git diff 查看
### git diff --cached <submodule name>只能看到概述，将 submodule 视为一个仓库提交
### git diff --cached --submodule 会显示子仓库的详细不同

## git submodule update --remote 更新子模块
### git fetch + git merge与上述命令等价

